package com.dpp.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.dpp.entity.*;
import com.dpp.mapper.ActivityMapper;
import com.dpp.mapper.CouponBookMapper;
import com.dpp.mapper.OrderMapper;
import com.dpp.properties.JwtProperties;
import com.dpp.result.Result;
import com.dpp.service.ActivityService;
import com.dpp.vo.Advert;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityRecordServiceImpl activityRecordService;
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private CouponBookMapper couponBookMapper;
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result getAdvert(String openId) {
        List<ActivityRecord> activityRecords =  activityRecordService.selectByListOpenid(openId,2);
        //2、activity_record表不为空，根据当前折扣卷的状态，返回广告
        if(!activityRecords.isEmpty()){
           int index = new Random().nextInt(activityRecords.size());
           ActivityRecord  activityRecord = activityRecords.get(index);
           Activity activity=  activityMapper.selectByActivityID(activityRecord.getActivityId());
            Advert advert=Advert.builder()
                    .advertImg(jwtProperties.getUrl().concat("/img/").concat(activity.getActivityImg()+""))
                    .advertUrl(jwtProperties.getUrl().concat(activity.getActivityUrl()+""))
                    .popping(activity.getPopping())
                    .poppingUrl(activity.getPoppingUrl()).build();
            return Result.success(advert);
        }
        //查找活动的优惠劵
        //List<CouponBook> couponBooks = couponBookMapper.selectListByOpenid(openId);
        //优惠劵使用完毕，表示活动结束
        //for (CouponBook couponBook : couponBooks) {
        //    if(couponBook.getCount()==0){
        //        return Result.success();
        //    }
        //}
        Advert advert=Advert.builder()
                .advertImg(jwtProperties.getUrl().concat("/img/").concat("person.jpg"))
                .advertUrl("")
                .popping(-1).build();
        return Result.success(advert);
        //return Result.success();
    }

    @Override
    public Result getCoupon(String openid) {
        //1、需要判断activity_record中，activity_id=2的活动是否完成了
        //完成了还调用这个函数，肯定是黑客，返回“已经领取过了”
        ActivityRecord activityRecord= activityRecordService.selectByActivityId(openid,2);
        if (activityRecord.getRecordFinished()==1){
            activityRecordService.updateEndRecord(2,openid);
            return Result.error("不是首次下的单");
        }
        CouponBook couponBook1 = couponBookMapper.selectByOpenidByCouponId(openid,1);
        if(couponBook1==null) {
            //添加到优惠券表中
            CouponBook couponBook = CouponBook.builder().openid(openid)
                    .count(3)
                    .couponId(CouponBook.ID1)
                    .build();
            couponBookMapper.save(couponBook);
            activityRecordService.updateEndRecord(2,openid);
            return Result.success("三张优惠券已放入卡包");
        }else{
            //添加到优惠券表中
            couponBookMapper.updateAddCount3(openid,CouponBook.ID1);
            activityRecordService.updateEndRecord(2,openid);
            return Result.success("三张优惠券已放入卡包");
        }
    }
    @Override
    public Result getCouponById2(String openid) {
        ActivityRecord activityRecord= activityRecordService.selectByActivityId(openid,4);
        if (activityRecord.getRecordFinished()==1){
            return Result.error("活动已参加");
        }
        CouponBook couponBook2 = couponBookMapper.selectByOpenidByCouponId(openid,2);
        if(couponBook2==null) {
            //添加到优惠券表中
            CouponBook couponBook = CouponBook.builder().openid(openid)
                    .count(3)
                    .couponId(CouponBook.ID2)
                    .build();
            couponBookMapper.save(couponBook);
            activityRecordService.updateEndRecord(4,openid);
            return Result.success("双十一优惠券已放入卡包");
        }else{
            //添加到优惠券表中
            couponBookMapper.updateAddCount3(openid,CouponBook.ID2);
            activityRecordService.updateEndRecord(4,openid);
            return Result.success("双十一优惠券已放入卡包");
        }
    }

}
